package com.banmao.algorithm.No701To900.No844;

public class Solution2 {

    public static boolean backspaceCompare(String s, String t) {

        char[] sChar = s.toCharArray();
        int sLength = sChar.length;
        char[] tChar = t.toCharArray();
        int tLength = tChar.length;

        // 逆序遍历两个字符串，同时记录退格数
        int skips = 0, skipt = 0;
        int sPoint = sLength - 1;
        int tPoint = tLength - 1;

        // 不要纠结于减少循环体
        while (sPoint >= 0 || tPoint >= 0) {
            while (sPoint >= 0) {
                // 对s进行退格操作
                if(sChar[sPoint] == '#') {
                    skips++;
                    sPoint--;
                } else if(skips > 0) {
                    skips--;
                    sPoint--;
                } else {
                    break;
                }
            }

            while (tPoint >= 0) {
                // 对t进行退格操作
                if(tChar[tPoint] == '#') {
                    skipt++;
                    tPoint--;
                } else if(skipt > 0) {
                    skipt--;
                    tPoint--;
                } else {
                    break;
                }
            }

            if(sPoint >= 0 && tPoint >= 0) {
                if(sChar[sPoint] != tChar[tPoint]) {
                    return false;
                }
            } else {
                if(sPoint >= 0 || tPoint >= 0) {
                    return false;
                }
            }

            sPoint--;
            tPoint--;

        }

        return true;
    }

}
